﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainProject.Quanlytuyen
{
    public partial class Quanlytuyen : Form
    {
        public Quanlytuyen()
        {
            InitializeComponent();
            loadData();
        }

        public void loadData()
        {
            QLXT_DBDataContext db = new QLXT_DBDataContext();
            //Lấy dữ liệu cho Gridview
            data_Tuyen.ClearSelection();
            var kh = from p in db.TUYENs select p;
            data_Tuyen.DataSource = kh;
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            Nhaptuyen nhap = new Nhaptuyen();
            nhap.ShowDialog(this);
            loadData();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Tuyen.SelectedRows;

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần chỉnh sửa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            DataGridViewRow r = rows[0];

            Suatuyen sua = new Suatuyen((int)r.Cells[0].Value);
            sua.ShowDialog(this);
            loadData();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            DataGridViewSelectedRowCollection rows = data_Tuyen.SelectedRows;
            QLXT_DBDataContext db = new QLXT_DBDataContext();

            if (rows.Count < 1)
            {
                MessageBox.Show("Hãy chọn dòng cần xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            foreach (DataGridViewRow r in rows)
            {
                TUYEN khdel = db.TUYENs.Single(p => p.IDTUYEN == (int)r.Cells[0].Value);
                db.TUYENs.DeleteOnSubmit(khdel);
            }

            db.SubmitChanges();

            loadData();
        }
    }
}
